package von.seiji.cn.test;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.math.MathUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.beust.jcommander.internal.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.time.DateUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.springframework.util.StreamUtils;
import org.yaml.snakeyaml.util.ArrayUtils;
import von.seiji.cn.SelfUtil;
import von.seiji.cn.asset.Charsets;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.math.BigDecimal;
import java.net.URL;
import java.nio.charset.Charset;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

public class T13 {

    @Test
    public void test1() throws IOException {
//        String s = SelfUtil.drawAscii(" .+*#$%&@", );
        File file = new File("D:\\111.jpg");
//        File file = new File("C:\\Users\\Administrator\\Pictures\\Saved Pictures\\天沢聖司.jpg");
        System.out.println(file.exists());
        String greychar = " .!*#%&@";

        StringBuilder builder = new StringBuilder();
        int length = greychar.length();
        BufferedImage image = ImageIO.read(file);
        int height = image.getHeight();
        int width = image.getWidth();

        for (int j = 0; j < height; j += 2) {
            for (int i = 0; i < width; ++i) {
                int rgb = image.getRGB(i, j);
                int r = rgb & 0xff;
                int g = rgb >> 8 & 0xff;
                int b = rgb >> 16 & 0xff;
                double v = (r * 299 + g * 587 + b * 114) / 1000D;
                builder.append(greychar.charAt((int) (v / 256 * length)));
            }
            builder.append(Charsets.LINE_SEPARATOR_WINDOWS);
        }
//        String s = SelfUtil.drawAscii(" .!*#%&@", file);
        FileUtil.writeString(builder.toString(), "C:\\Users\\Administrator\\Desktop\\新建文件夹\\1.txt", Charset.defaultCharset());
        System.out.println("完成！！！");
    }

    @Test
    public void test48() {
        double v = SelfUtil.scoreDeterminants(new double[][]{
                {43, 23, 64, 54},
                {41, 3.4, 3, 65},
                {74, 0.43, 0.43, 43},
                {34, 3, 13, 8}
        });
        System.out.println("v = " + v);
    }

    @Test
    public void test23() {
        long l = System.currentTimeMillis();
        System.out.println(l + "\t" + new Date(l));
        // 1637648405504 Tue Nov 23 14:20:05 CST 2021
        List<Date> dates = Lists.newArrayList(
                new Date(1637648515504l),
                new Date(1637648005504l),
                new Date(1637648405504l)
        );
        List<Date> collect = dates.stream().filter(item -> item.compareTo(new Date(l)) > 0).collect(Collectors.toList());
        System.out.println("collect = " + collect);

        Date date = dates.stream().min(Comparator.comparing(item -> item)).get();
        System.out.println(date);

       /* Date date1 = dates.stream().filter(item -> item.compareTo(new Date(l)) > 0)
                .max(Comparator.comparing(item -> item)).get();
        System.out.println(date1);*/

    }

    @Test
    public void test3232() {
        long l = System.currentTimeMillis();
        System.out.println(new Date(l).compareTo(new Date(l + 1000)));
    }


    @Test
    public void dlskajf() {
        int[] a = new int[]{2, 46, 7, 8, 3, 7, 79, 7, 6, 435, 321, 45, 4, 4};
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
        int i = Arrays.binarySearch(a, 3);
        System.out.println("i = " + i);
        int i1 = Arrays.binarySearch(a, 43);
        System.out.println("i1 = " + i1);
    }

    public static Class<?> extractModelClass(Class<?> mapperClass) {
        Type[] types = mapperClass.getGenericInterfaces();
        ParameterizedType target = null;
        for (Type type : types) {
            if (type instanceof ParameterizedType) {
                Type[] typeArray = ((ParameterizedType) type).getActualTypeArguments();
                if (ArrayUtil.isNotEmpty(typeArray)) {
                    for (Type t : typeArray) {
                        if (t instanceof TypeVariable || t instanceof WildcardType) {
                            break;
                        } else {
                            target = (ParameterizedType) type;
                            break;
                        }
                    }
                }
                break;
            }
        }
        return target == null ? null : (Class<?>) target.getActualTypeArguments()[0];
    }

    @Test
    public void alfd9() {
        List<Integer> integers = Lists.<Integer>newArrayList();
        Class<?> aClass = extractModelClass(integers.getClass());
        System.out.println("aClass = " + aClass);
        System.out.println("integers.getClass() = " + integers.getClass());
        System.out.println("integers.getClass().getComponentType() = " + integers.getClass().getComponentType());

    }


    @Test
    public void adlsa() {
        /**
         * 获取模版
         */
        // 获取配置，取生成订单快照和商品快照本地的根路径

        URL dash = ClassLoader.getSystemResource("dash");

        /**
         * 定义模版参数
         */
        Properties p = new Properties();
        // 指定模板的加载位置
        p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, new File(dash.getPath()).getAbsolutePath());
        // 指定输入编码
        p.setProperty(VelocityEngine.INPUT_ENCODING, "UTF-8");
        // 指定输出编码
        p.setProperty(VelocityEngine.OUTPUT_ENCODING, "UTF-8");
        // 设置日志信息
        p.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogChute");

        // 初始化模板引擎
        Velocity.init(p);

        // 获取VelocityContext
        VelocityContext context = new VelocityContext();


        String templ = "mapper.java.vm";
        Template template = Velocity.getTemplate(templ);
        System.out.println("template = " + template);
        StringWriter writer = new StringWriter();
        template.merge(context, writer);
        System.out.println("writer = " + writer);
    }

    @Test
    public void dla() {

        EasyExcel.write("C:\\Users\\Administrator\\Downloads\\11.xlsx"
                , DemoData.class).sheet("模板").doWrite(data());
    }

    private List<DemoData> data() {
        List<DemoData> list = ListUtils.newArrayList();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }

    @Test
    public void dskla() {
        Object o = Lists.newArrayList().stream().findAny().get();
        System.out.println("o = " + o);
    }

    @Test
    public void lkdsajf() {
        ResourceBundle type = ResourceBundle.getBundle("type");
        String[] aas = type.getStringArray("aa");
        System.out.println(Arrays.toString(aas));
    }

    @Test
    public void dlsfds() throws IOException {
        for (int i = 1;; i++) {
//            Document parse = Jsoup.parse(URLUtil.url("https://www.mingyantong.com/article/%E7%A7%92%E9%80%9F5%E5%8E%98%E7%B1%B3?page=" + i), 1000);
            String s = HttpUtil.get("https://www.mingyantong.com/article/%E7%A7%92%E9%80%9F5%E5%8E%98%E7%B1%B3?page=" + i);
            Document parse = Jsoup.parse(s);
            Elements elements = parse.select("a.xlistju");
            if(elements.isEmpty()) {
                break;
            }
//            System.out.println("-----------------------------------------------------------------------");
            elements.forEach(item -> {
                String text = item.text();
                System.out.println(text);
            });

        }
    }

    @Test
    public void fldskj() {
        LocalDate start = LocalDate.of(2021, 7, 14);
        LocalDate end = LocalDate.of(2022, 4, 17);

        List<Map<String, Object>> collect = LongStream.range(0, end.toEpochDay() - start.toEpochDay() + 1).mapToObj(item -> {
            String format = start.plusDays(item).format(
                    DateTimeFormatter.ISO_LOCAL_DATE
            );
            Map<String, Object> map = Maps.newHashMap();
            map.put("day", format);
            map.put("value", null);
            return map;
        }).collect(Collectors.toList());

//        collect.forEach(System.out::println);
//Duration.between(LocalDateTime.of(start, LocalTime.MIN), LocalDateTime.of(end, LocalTime.MIN)).toDays()
        SerializeWriter writer = new SerializeWriter(SerializerFeature.WRITE_MAP_NULL_FEATURES);
        JSON.writeJSONString(writer, collect);
        JSONArray array = JSON.parseArray(writer.toString());
        array.forEach(System.out::println);


    }

    @Test
    public void dflksf() {
        List<LocalDate> localDates = Lists.newArrayList(
                LocalDate.of(2021, 7, 14),
                LocalDate.of(2011, 8, 14)
        );
        List<LocalDate> localDates1 = Lists.newArrayList(
                LocalDate.of(2221, 7, 14),
                LocalDate.of(2511, 8, 14)
        );

        List<LocalDate> collect = Stream.concat(localDates.stream(), localDates1.stream())
                .collect(Collectors.toList());
        System.out.println("collect = " + collect);

        List<LocalDate> collect1 = Stream.of(localDates, localDates1).flatMap(List::stream)
                .collect(Collectors.toList());
        System.out.println("collect1 = " + collect1);

        Stream.iterate(1, item -> item * 2)
                .limit(10).collect(Collectors.toList())
                .forEach(System.out::println);

        LocalDate date = localDates.stream().min(Comparator.comparing(LocalDate::toEpochDay)).get();
        System.out.println("date = " + date);

        boolean contains = localDates.contains(LocalDate.of(2021, 7, 14));
        System.out.println("contains = " + contains);
    }


}
